const svgCaptcha = require('svg-captcha')
const { uuid } = require('uuidv4')
const client = require('../utils/redisClient')
const catchAsync = require('../utils/catchAsync')

const generateCaptcha = catchAsync(async (req, res, next) => {
  const captcha = svgCaptcha.create({
    size: 4,
    noise: 2,
    color: true,
    background: '#cc9966',
    width: 150,
    height: 40,
  })

  const id = uuid()
  await client.setEx(`captcha:${id}`, 120, captcha.text.toLowerCase())

  // 返回captcha的路径数据
  res.status(200).json({
    status: 'success',
    data: {
      id,
      captcha: captcha.data,
    },
  })
})

module.exports = generateCaptcha
